package views;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListModel;

import javax.swing.WindowConstants;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.SwingUtilities;
import com.cloudgarden.layout.AnchorConstraint;
import com.cloudgarden.layout.AnchorLayout;

import controllers.Buscar_Paciente_Controller;
import controllers.MyPacientesTableModel;


import models.*;
import java.util.*;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class PantallaBuscarPaciente extends javax.swing.JFrame {

	{//set look and feel
		try {
			javax.swing.UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
		} catch(Exception e) {
			e.printStackTrace();
		}
	}

	private JScrollPane conteinerScroll;
	private JButton btnCancelar;
	private JButton btnSeleccionar;
	private JTable tablaPacientes;
	private JLabel lblPacientes;

	private Actions l= new Actions();
	private Pantalla_Turnos parent;
	private MyPacientesTableModel modelPacientes;
	private Buscar_Paciente_Controller controller= Buscar_Paciente_Controller.getInstancia();
	public PantallaBuscarPaciente(Pantalla_Turnos parent) {
		super();
		this.parent=parent;
		initGUI();
	}
	
	private void initGUI() {
		try {
			AnchorLayout thisLayout = new AnchorLayout();
			getContentPane().setLayout(thisLayout);
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			this.setTitle("buscar paciente");
			this.setPreferredSize(new java.awt.Dimension(795, 433));
			{
				btnSeleccionar = new JButton();
				getContentPane().add(btnSeleccionar, new AnchorConstraint(871, 759, 978, 571, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
				btnSeleccionar.setText("Seleccionar");
				btnSeleccionar.setPreferredSize(new java.awt.Dimension(93, 28));
				btnSeleccionar.addActionListener(l);
			}
			{
				btnCancelar = new JButton();
				getContentPane().add(btnCancelar, new AnchorConstraint(871, 972, 978, 783, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
				btnCancelar.setText("Cancelar");
				btnCancelar.setPreferredSize(new java.awt.Dimension(94, 28));
				btnCancelar.addActionListener(l);
			}
			{
				lblPacientes = new JLabel();
				getContentPane().add(lblPacientes, new AnchorConstraint(120, 384, 181, 69, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
				lblPacientes.setText("Seleccione el paciente");
				lblPacientes.setPreferredSize(new java.awt.Dimension(121, 16));
			}
			{
				conteinerScroll = new JScrollPane();
				getContentPane().add(conteinerScroll, new AnchorConstraint(205, 759, 786, 69, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
				conteinerScroll.setPreferredSize(new java.awt.Dimension(342, 151));
				{
					tablaPacientes = new JTable();
					conteinerScroll.setViewportView(tablaPacientes);
				
				}
			}
			pack();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public class Actions implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			Object source = e.getSource();
			if(source == btnCancelar)
			{
				parent.setEnabled(true);
				dispose();
			}
			else if(source== btnSeleccionar)
			{
				if(tablaPacientes.getSelectedRowCount() == 1 )
				{
					Paciente seleccionado= modelPacientes.getPacienteAt(tablaPacientes.getSelectedRow());
					controller.setPacienteActual(seleccionado);
					JOptionPane.showMessageDialog(null,"Usted ha seleccionado al paciente: " + seleccionado.getNombre() + " " + seleccionado.getApellido()  );
					dispose();
					parent.setEnabled(true);
					parent.visibilidadPacienteSeleccionado(true, seleccionado.getNombre() + " " + seleccionado.getApellido());
				}
				else if(tablaPacientes.getSelectedRowCount() == 0 )
				{
					JOptionPane.showMessageDialog(null, "Debe seleccionar un paciente");
				}
				else
				{
					JOptionPane.showMessageDialog(null, "Solo puede seleccionar un paciente a la vez");
				}
			}
		}
	}
	public void setTableModel(Vector<Paciente> pacientes)
	{
		modelPacientes= new MyPacientesTableModel(pacientes);
		tablaPacientes.setModel(modelPacientes);
	}

}
